<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>查漏补缺 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/21.3f0b334e.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>面向对象</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/294c58/" class="sidebar-link">类与对象和包</a></li><li><a href="/pages/6db17e/" class="sidebar-link">继承与抽象</a></li><li><a href="/pages/637d05/" class="sidebar-link">接口与多态</a></li><li><a href="/pages/efafe7/" aria-current="page" class="active sidebar-link">查漏补缺</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/efafe7/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_1-1-今日学习内容" class="sidebar-link">1.1 今日学习内容</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_1-2-今日学习目标" class="sidebar-link">1.2 今日学习目标</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/efafe7/#二、this关键字" class="sidebar-link">二、this关键字</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_2-1-this-内存图-理解" class="sidebar-link">2.1 this 内存图（理解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_1-2-this-三种用法-重点掌握" class="sidebar-link">1.2 this 三种用法（重点掌握）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/efafe7/#三、super关键字" class="sidebar-link">三、super关键字</a></li><li class="sidebar-sub-header level2"><a href="/pages/efafe7/#四、static-修饰符" class="sidebar-link">四、static 修饰符</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_4-1-static-修饰符" class="sidebar-link">4.1 static 修饰符</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_4-2-静态变量" class="sidebar-link">4.2  静态变量</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_4-3-静态方法" class="sidebar-link">4.3 静态方法</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_4-4-final修饰符-掌握" class="sidebar-link">4.4 final修饰符（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_4-5-代码块-了解" class="sidebar-link">4.5 代码块 (了解)</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_4-6-内部类" class="sidebar-link">4.6. 内部类</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_4-6-1-内部类分类-了解" class="sidebar-link">4.6.1 内部类分类 ( 了解 )</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_4-6-2-匿名内部类-重点掌握" class="sidebar-link">4.6.2 匿名内部类 (重点掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_4-7-枚举类-掌握" class="sidebar-link">4.7 枚举类（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_4-7-1-枚举类的定义和使用-掌握" class="sidebar-link">4.7.1. 枚举类的定义和使用（掌握）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/efafe7/#五、小结与作业" class="sidebar-link">五、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_5-1-小结" class="sidebar-link">5.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/efafe7/#_5-2-作业" class="sidebar-link">5.2 作业</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-1-题目1" class="sidebar-link">5.2.1 题目1</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-2-题目2" class="sidebar-link">5.2.2 题目2</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-3-题目3" class="sidebar-link">5.2.3 题目3</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-4-题目4" class="sidebar-link">5.2.4 题目4</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-5-题目5" class="sidebar-link">5.2.5 题目5</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-6-题目6" class="sidebar-link">5.2.6 题目6</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-7-题目7" class="sidebar-link">5.2.7 题目7</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-8-题目8" class="sidebar-link">5.2.8 题目8</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-9-题目9" class="sidebar-link">5.2.9 题目9</a></li><li class="sidebar-sub-header level4"><a href="/pages/efafe7/#_5-2-10-题目10" class="sidebar-link">5.2.10 题目10</a></li></ul></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1" title="分类" data-v-06225672>面向对象</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-02-28</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">查漏补缺<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="查漏补缺"><a href="#查漏补缺" class="header-anchor">#</a> 查漏补缺</h1> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <h3 id="_1-1-今日学习内容"><a href="#_1-1-今日学习内容" class="header-anchor">#</a> 1.1 今日学习内容</h3> <ul><li>this关键字</li> <li>super关键字</li> <li>static、final修饰符</li> <li>代码块</li> <li>内部类</li> <li>枚举类</li></ul> <h3 id="_1-2-今日学习目标"><a href="#_1-2-今日学习目标" class="header-anchor">#</a> 1.2 今日学习目标</h3> <ul><li>重点掌握this关键字的含义和用法</li> <li>重点掌握super关键字的含义和用法</li> <li>掌握static修饰符的含义</li> <li>掌握final修饰符的修饰类，方法，变量的含义</li> <li>代码块
<ul><li>了解代码块有那些</li> <li>掌握静态代码块的语法</li></ul></li> <li>内部类
<ul><li>了解内部类有哪些</li> <li>掌握匿名内部类的语法(难点)</li></ul></li> <li>掌握枚举类的定义和使用</li></ul> <h2 id="二、this关键字"><a href="#二、this关键字" class="header-anchor">#</a> 二、this关键字</h2> <h3 id="_2-1-this-内存图-理解"><a href="#_2-1-this-内存图-理解" class="header-anchor">#</a> 2.1 this 内存图（理解）</h3> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/DsXP5MCUwBpKO3u.png" alt=""></em></p> <p>this关键字表示当前对象本身，一般用于类的内部，this中存在一个地址，引用当前创建的对象本身。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThisDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>		
		<span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;stu = &quot;</span> <span class="token operator">+</span> stu<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>当new一个对象时，实际上产生了两个引用，一个是供类Student内部调用其成员变量和成员方法的this关键字，一个是供程序外部调用对象成员的stu。</p> <h3 id="_1-2-this-三种用法-重点掌握"><a href="#_1-2-this-三种用法-重点掌握" class="header-anchor">#</a> 1.2 this 三种用法（重点掌握）</h3> <p><strong>[1] 调用成员变量（掌握）</strong></p> <p>解决局部变量和成员变量二义性问题，此时必须使用</p> <p><strong>[2] 调用其他成员方法（掌握）</strong></p> <p>同一个类中非static方法间互调（此时可以省略this，但是不建议省略）</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;大家好，以下是我的信息:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>[3] 构造器的互调（看懂即可）</strong></p> <p>this可以调用本类其他构造方法，语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">this</span><span class="token punctuation">(</span>实参<span class="token number">1</span><span class="token punctuation">,</span>实参<span class="token number">2</span><span class="token punctuation">,</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>注意 :  构造器的调用必须写在第一句，否则出现编译错误。</p> <p>案例：一个结合this关键字，满足JavaBean规范实战中的Student类</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 调用两个参数的构造器,必须放在构造器第一行</span>
		<span class="token comment">// TODO其他操作</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

    <span class="token comment">// 省略setter and getter</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;大家好:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p>什么时候需要使用this：this主要存在于两个位置：</p> <ul><li><p>在构造器中：表示当前被创建的对象</p></li> <li><p>在方法中：哪一个对象调用this所在的方法，此时this就表示哪一个对象(<strong>谁调用方法，this就是谁</strong>)</p></li></ul> <h2 id="三、super关键字"><a href="#三、super关键字" class="header-anchor">#</a> 三、super关键字</h2> <p>super 含义：</p> <p>this		：表示当前对象</p> <p><strong>super	：当前对象的父类对象</strong></p> <p>什么时候使用super:   <strong>需要在子类中调用父类的成员时，一定使用super</strong></p> <ul><li><p>在子类方法中，调用父类被覆盖的方法，此时必须使用super</p></li> <li><p>在子类构造器中，调用父类构造器，此时必须使用super语句</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>语法：
<span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">super</span><span class="token punctuation">(</span>参数<span class="token number">1</span>，参数<span class="token number">2.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意：在创建子类对象时，在子类构造器的第一行必须先调用父类的构造器（如果没调用任何构造器，编译默认加<code>super()</code>  ）</p></li></ul> <p>需求：完成继承时的需求</p> <p>父类代码：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
    
	<span class="token keyword">public</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
    
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>子类代码：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token keyword">extends</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> sn<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 显示调用父类无参数构造器,必须作为构造器第一行，如果没有写，编译器编译时会自动加super()</span>
	<span class="token punctuation">}</span>
    
	<span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">,</span> <span class="token class-name">String</span> sn<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 显示去调用父类无参数构造器,必须作为构造第一行</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>sn <span class="token operator">=</span> sn<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//?此时调用谁的方法</span>
		<span class="token comment">// this.showInfo();		//?此时调用谁的方法</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>sn<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>综合案例：实战开发中，结合this，super，JavaBean规范、继承下的类。请用面向对象知识构建以下类信息。</p> <table><thead><tr><th>类型</th> <th>字段</th> <th></th> <th></th> <th>行为</th></tr></thead> <tbody><tr><td>狗狗</td> <td>昵称</td> <td>健康值</td> <td>品种</td> <td>输出基本信息 showInfo ( 打印昵称,健康值,品种 )</td></tr> <tr><td>猫猫</td> <td>昵称</td> <td>健康值</td> <td>性别</td> <td>输出基本信息 showInfo (打印昵称,健康值,性别 )</td></tr></tbody></table> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// Pet 宠物</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Pet</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> nick<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> health<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getNick</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> nick<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setNick</span><span class="token punctuation">(</span><span class="token class-name">String</span> nick<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>nick <span class="token operator">=</span> nick<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getHealth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> health<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setHealth</span><span class="token punctuation">(</span><span class="token keyword">int</span> health<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>health <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;健康值不合法&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>health <span class="token operator">=</span> health<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Pet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Pet</span><span class="token punctuation">(</span><span class="token class-name">String</span> nick<span class="token punctuation">,</span> <span class="token keyword">int</span> health<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>nick <span class="token operator">=</span> nick<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setHealth</span><span class="token punctuation">(</span>health<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我的名字:&quot;</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>nick<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我的健康值:&quot;</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>health<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><p>使用super关键字</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Dog</span> <span class="token keyword">extends</span> <span class="token class-name">Pet</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> strain<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getStrain</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> strain<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setStrain</span><span class="token punctuation">(</span><span class="token class-name">String</span> strain<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>strain <span class="token operator">=</span> strain<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token class-name">String</span> nick<span class="token punctuation">,</span> <span class="token keyword">int</span> health<span class="token punctuation">,</span> <span class="token class-name">String</span> strain<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span>nick<span class="token punctuation">,</span> health<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>strain <span class="token operator">=</span> strain<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;strain:&quot;</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>strain<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h2 id="四、static-修饰符"><a href="#四、static-修饰符" class="header-anchor">#</a> 四、static 修饰符</h2> <h3 id="_4-1-static-修饰符"><a href="#_4-1-static-修饰符" class="header-anchor">#</a> 4.1 static 修饰符</h3> <p>static 修饰符表示静态，可以修饰成员变量构成静态成员变量，修饰成员方法构成静态成员方法。</p> <p>静态成员变量和静态成员方法，称为类的静态成员，都<strong>归类</strong>所有</p> <h3 id="_4-2-静态变量"><a href="#_4-2-静态变量" class="header-anchor">#</a> 4.2  静态变量</h3> <p>在类中，用static修饰符修饰的成员变量称为静态成员变量，归类所有，语法 :</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> <span class="token keyword">static</span> 数据类型 变量名<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>静态变量的访问语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>类名<span class="token punctuation">.</span>静态变量名		 <span class="token comment">// 推荐写法</span>
对象名<span class="token punctuation">.</span>静态变量名		<span class="token comment">// 底层用 类名.静态变量名 方式访问</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_4-3-静态方法"><a href="#_4-3-静态方法" class="header-anchor">#</a> 4.3 静态方法</h3> <p>static 也可以修饰成员方法称为静态成员方法，归类所有。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> <span class="token keyword">static</span> 返回值类型 方法名<span class="token punctuation">(</span>形参列表<span class="token punctuation">)</span><span class="token punctuation">{</span>
    方法体<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>调用静态方法语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>类名<span class="token punctuation">.</span>静态方法名<span class="token punctuation">(</span><span class="token punctuation">)</span>   <span class="token comment">// 推荐写法</span>
对象名<span class="token punctuation">.</span>静态方法名<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// 底层用 类名.静态方法名 方式访问</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
   <span class="token comment">// 成员变量</span>
   <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
   <span class="token comment">// 静态成员变量</span>
   <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> city <span class="token operator">=</span> <span class="token string">&quot;广州&quot;</span><span class="token punctuation">;</span>

   <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token keyword">return</span> name<span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token comment">// 成员方法</span>
   <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">showInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我的名字:&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">&quot;,我在&quot;</span> <span class="token operator">+</span> <span class="token class-name">Student</span><span class="token punctuation">.</span>city<span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token comment">// 静态成员方法</span>
   <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">getCityName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token keyword">return</span> <span class="token class-name">Student</span><span class="token punctuation">.</span>city<span class="token punctuation">;</span>
   <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><blockquote><p>此时，观察发现类中的成员
按照是否被static修饰来分：分为静态成员和非静态成员
静态成员（静态变量、静态方法等），归类所有，推荐使用类名来调用
非静态成员，归对象所有，必须通过实例（对象）来调用</p></blockquote> <p><strong>记住结论 ( 十分重要 )：</strong></p> <ul><li><p><strong>静态成员：使用static修饰的字段和方法：	属于类		直接使用类名调用即可</strong></p></li> <li><p><strong>非静态成员：没用static修饰的字段和方法：	属于对象	必须先创建对象，再调用</strong></p></li></ul> <p>注意：static方法不能使用super和this</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>因为<span class="token keyword">static</span>是类级别的，<span class="token keyword">super</span>和<span class="token keyword">this</span>是对象级别的
存在类的时候不一定存在对象，也就说使用类名调用<span class="token keyword">static</span>方法时，此时可能是没有对象的。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>内存分析：</p> <p>static修饰的成员，优先于对象创建，也同时存储在方法区中，被该类的所有对象共享。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/uCPOk2WlprLcTd6.png" alt=""></p> <h3 id="_4-4-final修饰符-掌握"><a href="#_4-4-final修饰符-掌握" class="header-anchor">#</a> 4.4 final修饰符（掌握）</h3> <p>子类可以继承父类的实现细节，也可以通过方法覆盖的形式修改功能实现细节。那么怎么来限制某个类不能有子类，不能覆盖方法？——final修饰符</p> <p>final的含义是最终的，不可改变的，可以修饰类、方法、变量。</p> <ul><li>最终类。final修饰的类：表示最终的类, 该类不能再有子类。语法：</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">SuperA</span> <span class="token punctuation">{</span>
	
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Sub</span>  <span class="token keyword">extends</span> <span class="token class-name">SuperA</span> <span class="token punctuation">{</span>	<span class="token comment">//此行语法报错</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>最终方法。final修饰的方法：最终的方法,该方法不能被子类覆盖</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SuperA</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Sub</span>  <span class="token keyword">extends</span> <span class="token class-name">SuperA</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>		<span class="token comment">//此行语法报错</span>
	
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li><p>常量。被final修饰的变量称为常量，常量只能赋值一次，不能再重新赋值。常量命名建议：全大写，多个单词用下划线分割</p> <ul><li>基本数据类型：存储的值不能改变</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">final</span> <span class="token keyword">int</span> <span class="token constant">MAX_VALUE</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token constant">MAX_VALUE</span> <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>		<span class="token comment">// 此行语法报错</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>引用数据类型：存储的地址值不能改变。</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token keyword">final</span> <span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 stu<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">// stu对象的成员变量是可以改变的	</span>
 stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">// 此行语法报错</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li></ul> <h3 id="_4-5-代码块-了解"><a href="#_4-5-代码块-了解" class="header-anchor">#</a> 4.5 代码块 (了解)</h3> <p>什么是代码块：直接使用{ }括起来的一段代码区域。</p> <p>根据位置的不同，存在三种形式：</p> <ul><li>局部代码块： 直接定义在方法内部的代码块，一般的，不会直接使用局部代码块的，结合if、while、for等关键字使用，表示一块代码区域。代码块中的变量属于局部变量，只在自己所在区域{ }内有效。</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CodeBlockDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;begin...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">{</span>
			<span class="token comment">// 直接使用代码块，一般不用</span>
			<span class="token keyword">int</span> age <span class="token operator">=</span> <span class="token number">17</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//此行报错,超出age作用范围,就不能访问到了</span>
        
		<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> item <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;end...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><ul><li><p>初始化代码块（构造代码块）：直接定义在类中。每次创建对象的时候都会执行初始化代码块，开发中不使用初始化代码块，即使要做初始化操作，可以直接在构造器中完成即可。</p></li> <li><p><strong>静态代码块</strong>：使用static修饰的初始化代码块，当代码块所在类的字节码被加载进JVM内存时，就执行static代码块中的代码。<strong>在开发中，用来做加载资源、加载配置文件等操作</strong>。</p></li></ul> <p>分析下面代码执行顺序：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token comment">// 静态代码块</span>
    <span class="token keyword">static</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;静态代码块&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 构造代码块</span>
    <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;构造代码块&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;构造器&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>测试静态代码块的执行过程</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CodeBlockDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span> stu2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>运行结果：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>静态代码块<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
初始化代码块<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
构造器
初始化代码块<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
构造器
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_4-6-内部类"><a href="#_4-6-内部类" class="header-anchor">#</a> 4.6. 内部类</h3> <h4 id="_4-6-1-内部类分类-了解"><a href="#_4-6-1-内部类分类-了解" class="header-anchor">#</a> 4.6.1 内部类分类 ( 了解 )</h4> <p>什么是内部类，把一个类定义在另一个类的内部，把里面的类称之为内部类，把外面的类称之为外部类。（能认识内部类即可）</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/CTHPrElDeyWSvkR.png" alt=""></strong></p> <p><strong>内部类可以看作和成员变量、方法一样，是外部类的成员</strong>，而且成员可以有static修饰。</p> <ul><li><p>静态内部类：使用static修饰的内部类，那么访问内部类直接使用外部类名来访问</p></li> <li><p>实例(成员)内部类：没有使用static修饰的内部类，访问内部类使用外部类的对象来访问</p></li> <li><p>局部(方法)内部类：定义在方法中的内部类，一般不用</p></li> <li><p>匿名内部类：特殊的局部内部类，适合于仅使用一次使用的类</p></li></ul> <p>对于每个内部类来说，Java编译器会生成独立.class文件。</p> <ul><li><p>静态和实例内部类：外部类名$内部类名字</p></li> <li><p>局部内部类：外部类名$数字内部类名称</p></li> <li><p>匿名内部类：外部类名$数字</p></li></ul> <p>只需要记住：如果一个类字节码名字中含有$，那么它存在内部类</p> <h4 id="_4-6-2-匿名内部类-重点掌握"><a href="#_4-6-2-匿名内部类-重点掌握" class="header-anchor">#</a> 4.6.2 匿名内部类 (重点掌握)</h4> <p>匿名内部类也就是没有名字的内部类，正因为没有名字，所以匿名内部类只能使用一次，它通常用来简化代码编写。</p> <p>在多态应用案例中，如果雇员还可以通过轿车回家，那么我们需要新增一个Car类，此时需要单独使用一个文件来定义Car类。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Car</span> <span class="token keyword">implements</span> <span class="token class-name">IRunnable</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;小轿车在跑&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>创建多态对象，传入goHomeBy中</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PolyDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Employee</span> employee <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Employee</span><span class="token punctuation">(</span><span class="token string">&quot;will&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">IRunnable</span> runnable <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Car</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        employee<span class="token punctuation">.</span><span class="token function">goHomeBy</span><span class="token punctuation">(</span>runnable<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>如果这个Car类只使用一次的话，就完全没有必要单独定义一个Java文件，直接使用匿名内部类来完成。</p> <p>使用匿名内部类还有个<strong>前提条件</strong>：必须存在继承一个父类或<strong>实现一个接口</strong></p> <p>匿名内部类，可以使用父类构造器和接口名来完成。</p> <p>针对类，定义匿名内部类来继承父类（使用较少）：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">new</span> 父类构造器<span class="token punctuation">(</span><span class="token punctuation">[</span>实参列表<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
    <span class="token comment">// 匿名内部类的类体部分 </span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>针对接口，定义匿名内部类来实现接口（使用最多）：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>接口类型 变量 <span class="token operator">=</span> <span class="token keyword">new</span> 接口名<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    <span class="token comment">// 匿名内部类的类体部分</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意：这里不是根据 父类/接口 创建对象，而是一种语法而已</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">IRunnable</span> runnable <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">IRunnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;小轿车在跑&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
employee<span class="token punctuation">.</span><span class="token function">goHomeBy</span><span class="token punctuation">(</span>runnable<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_4-7-枚举类-掌握"><a href="#_4-7-枚举类-掌握" class="header-anchor">#</a> 4.7 枚举类（掌握）</h3> <h4 id="_4-7-1-枚举类的定义和使用-掌握"><a href="#_4-7-1-枚举类的定义和使用-掌握" class="header-anchor">#</a> 4.7.1. 枚举类的定义和使用（掌握）</h4> <p>当一个变量有几种固定可能的取值时，就可以将它定义为枚举类型，枚举是一种特殊的类</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">enum</span> 枚举名 <span class="token punctuation">{</span>
    枚举项<span class="token number">1</span><span class="token punctuation">,</span>
    枚举项<span class="token number">2</span><span class="token punctuation">,</span>
    枚举项<span class="token number">3</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 枚举名使用大写驼峰</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>定义枚举：</p> <p>需求：定义性别的枚举。性别的取值为：男、女</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">enum</span> <span class="token class-name">Gender</span> <span class="token punctuation">{</span>
	<span class="token constant">MEN</span><span class="token punctuation">,</span> 
    <span class="token constant">WOMEN</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>使用枚举 :  枚举是一种数据类型，可以定义变量，变量的取值必须是枚举项中的其中一个</p> <ul><li>通过枚举名直接引用枚举项即可。取值语法：枚举名.枚举项</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Gender</span> gender <span class="token operator">=</span> <span class="token class-name">Gender</span><span class="token punctuation">.</span><span class="token constant">MEN</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>注意 :</p> <ul><li>枚举其实本质上是一个类，每一个枚举项是本枚举类类型的一个对象。</li> <li>枚举类不能使用创建对象</li></ul> <blockquote><p>提示：反编译字节码</p> <p>javap -p 字节码名</p></blockquote> <p>目前，会定义枚举类和基本使用就可以了，后面还会讲更高级的使用方式。</p> <h2 id="五、小结与作业"><a href="#五、小结与作业" class="header-anchor">#</a> 五、小结与作业</h2> <h3 id="_5-1-小结"><a href="#_5-1-小结" class="header-anchor">#</a> 5.1 小结</h3> <ul><li>this 关键字-概念+用法-建议5遍</li> <li>super 关键字-概念+用法-建议5遍</li> <li>static 关键字-概念+用法-建议5遍</li> <li>final 关键字-概念+用法-建议5遍</li> <li>代码块-概念+用法-建议5遍</li> <li>内部类-概念+用法-建议5遍</li> <li>枚举类-概念+用法-建议5遍</li></ul> <h3 id="_5-2-作业"><a href="#_5-2-作业" class="header-anchor">#</a> 5.2 作业</h3> <h4 id="_5-2-1-题目1"><a href="#_5-2-1-题目1" class="header-anchor">#</a> 5.2.1 题目1</h4> <p>需求：一个结合this关键字，满足JavaBean规范实战中的Dog类</p> <ul><li>Dog类包含id、姓名、年龄字段，具有sayHi方法</li> <li>Dog类提供Dog( String id ,String name )  和 Dog(String id, String name, int age) 的构造器</li> <li>提供showInfo方法（打印id、姓名、年龄信息）</li> <li>提供sayHi方法，调用showInfo方法输出基本信息</li></ul> <p>作业目的：理解this关键字的含义和使用</p> <h4 id="_5-2-2-题目2"><a href="#_5-2-2-题目2" class="header-anchor">#</a> 5.2.2 题目2</h4> <p>需求1：请用面向对象知识构建以下类信息。</p> <table><thead><tr><th>类型</th> <th>字段</th> <th></th> <th></th> <th>行为</th></tr></thead> <tbody><tr><td>狗狗</td> <td>昵称</td> <td>健康值</td> <td>品种</td> <td>输出信息</td></tr> <tr><td>猫猫</td> <td>昵称</td> <td>健康值</td> <td>性别</td> <td>输出信息</td></tr></tbody></table> <p>结合this，super，JavaBean规范、继承相关知识，完成以上类的定义。</p> <h4 id="_5-2-3-题目3"><a href="#_5-2-3-题目3" class="header-anchor">#</a> 5.2.3 题目3</h4> <p>需求：定义一个狗类Dog，在狗类中定义静态字段和静态方法（属于类的成员），定义非静态字段和非静态方法（属于对象的成员），并在StaticDemo方法中测试调用狗类中的方法</p> <p>作业目的：理解static关键字的含义和使用</p> <h4 id="_5-2-4-题目4"><a href="#_5-2-4-题目4" class="header-anchor">#</a> 5.2.4 题目4</h4> <p>需求：演示final修饰符的特点</p> <ul><li><p>final修饰的类：表示最终的类, 该类不能再有子类</p></li> <li><p>final修饰的方法：最终的方法, 该方法不能被子类覆盖</p></li> <li><p>final修饰的变量：表示常量，该变量只能赋值一次，不能再重新赋值。</p></li> <li><p>基本数据类型：表示的值不能改变</p></li> <li><p>引用数据类型：所引用的地址值不能改变</p></li></ul> <p>作业目的：final修饰符的特点</p> <h4 id="_5-2-5-题目5"><a href="#_5-2-5-题目5" class="header-anchor">#</a> 5.2.5 题目5</h4> <p>需求：演示静态代码块、初始化代码块、普通代码块的写法</p> <p>作业目的：掌握静态代码块的语法</p> <h4 id="_5-2-6-题目6"><a href="#_5-2-6-题目6" class="header-anchor">#</a> 5.2.6 题目6</h4> <p>需求：演示匿名内部类用法</p> <ul><li>定义一个接口IUSB</li> <li>在测试类中IUSB的实现类匿名内部类</li></ul> <p>作业目的：掌握匿名内部类的语法和使用</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/* 
总结
1&gt; 什么时候使用匿名内部类？使用前提？
2&gt; 匿名内部类语法和各部分语法的含义
*/</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="_5-2-7-题目7"><a href="#_5-2-7-题目7" class="header-anchor">#</a> 5.2.7 题目7</h4> <p>需求：定义一个ClothType枚举类，表示衣服分类的三种情况：男装、女装、中性服装</p> <p>并在测试类中获取枚举中的常量对象，并调用枚举中的name、ordinal方法。</p> <p>作业目的：定义枚举类和枚举的使用</p> <h4 id="_5-2-8-题目8"><a href="#_5-2-8-题目8" class="header-anchor">#</a> 5.2.8 题目8</h4> <p>需求:</p> <p>商品类: Item</p> <p>具有成员变量,:</p> <p>​	编号(code)  String类型  商品编号</p> <p>​	名称(name)  String 类型  商品名称</p> <p>​	类别(type)  String 类型  商品的具体类别</p> <p>​	价格(price)  double 类型 商品的价格</p> <p>​	<strong>总商品数量（totalNum） int 类型 商品的总商品数量</strong></p> <p>1)提供两个构造器</p> <p>​	没有参数的构造器</p> <p>​	创建一个以编号(code),名称(name) 作为形式参数的构造器</p> <p>2)编写一个main方法,添加一个商品,其商品信息如下</p> <p>​	创建两个笔记本电脑的商品(computer),</p> <p>商品1：</p> <p>​	编号(code):666</p> <p>​	名称: 戴尔(DELL) 游戏笔记本</p> <p>​	类型: 游戏</p> <p>​	价格: 5499.00</p> <p>商品2：</p> <p>​	编号(code):007</p> <p>​	名称: 苹果（Macbook pro）笔记本</p> <p>​	类型: 电脑,办公</p> <p>​	价格: 18000.00</p> <p>3）在main方法打印笔记本电脑(computer)的名称和价格</p> <p>4）每创建一个商品对象总商品数量（totalNum）加一，请在main方法中打印totalNum值。</p> <h4 id="_5-2-9-题目9"><a href="#_5-2-9-题目9" class="header-anchor">#</a> 5.2.9 题目9</h4> <p>按以下要求编写一个程序：</p> <p>1)请用javaBean规范设计一个学生类Student，具有：</p> <p>​	字段：no(学号)、name(姓名)和score(成绩)</p> <p>​	功能：输出一位学生的数据内容(toString)、<strong>计算学生的平均分(getAvg)</strong></p> <p>2)在main()方法中创建五个学生信息，并定义一个对象数组用于存储创建的五位学生的数据，</p> <p>​	使用<strong>Student类的getAvg方法</strong>,  传入对象数组并 计算出这些学生的平均分；并打印所有学生的信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token class-name">Student</span><span class="token punctuation">[</span><span class="token punctuation">]</span> stuArr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>在Student类中定义getAvg()方法， 并接收Student[]数组类型的参数，计算传递进来的学生的平均成绩</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token comment">// </span>
    <span class="token comment">// </span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">double</span> <span class="token function">getAvg</span><span class="token punctuation">(</span><span class="token class-name">Student</span><span class="token punctuation">[</span><span class="token punctuation">]</span> students<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// step 1: 定义一个变量sum，存储求和</span>
        <span class="token keyword">double</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token comment">// step 2: 遍历数组，取出每个学生对象，获取学生的成绩，累加到sum中</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Student</span> stu<span class="token operator">:</span>students<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            sum <span class="token operator">+=</span> stu<span class="token punctuation">.</span><span class="token function">getScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// step 3：求平均值</span>
        <span class="token keyword">return</span> sum <span class="token operator">/</span> students<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Student</span> s1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;001&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span> s2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;002&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">,</span><span class="token number">99</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span> s3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;003&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">,</span><span class="token number">97</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span> s4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;004&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span> s5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;005&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token class-name">Student</span><span class="token punctuation">[</span><span class="token punctuation">]</span> students <span class="token operator">=</span> <span class="token punctuation">{</span> s1<span class="token punctuation">,</span>s2<span class="token punctuation">,</span>s3<span class="token punctuation">,</span>s4<span class="token punctuation">,</span>s5 <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span><span class="token punctuation">.</span><span class="token function">getAvg</span><span class="token punctuation">(</span>students<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 总结经验
 * 在定义方法时，如何界定方法设计成静态还是非静态？
 * 当一个方法的实现过程不需要调用对象的数据(字段)，也不需要使用this等，就可以设计成静态的
 * 否则，如果需要用带字段数据或者this，一定要设计成非静态的
 */</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h4 id="_5-2-10-题目10"><a href="#_5-2-10-题目10" class="header-anchor">#</a> 5.2.10 题目10</h4> <p>定义一个网络用户类(User)，包含用户id(id)、用户密码(password)、email地址(emailAdrr) 3个私有化成员变量和一个<strong>属于类的用户总人数(totalUser)</strong>，</p> <p>(1)、请定义一个含有用户ID(ID)、用户密码（password）两个参数的构造器，完成三个实例成员变量的初始化，其中email地址（emailAdrr）是用户ID加上字符串<code>@gameschool.com</code>。</p> <p>(3)、请利用上述构造器，在main方法中实例化网络用户类对象，每创建一个对象，用户总人数(totalUser)加一，创建三个实例，最后打印用户总人数。</p></div></div>  <div class="page-edit"><!----> <!----> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/637d05/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">接口与多态</div></a> <a href="/pages/d3f979/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">常用类-简单类</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/637d05/" class="prev">接口与多态</a></span> <span class="next"><a href="/pages/d3f979/">常用类-简单类</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/21.3f0b334e.js" defer></script>
  </body>
</html>
